package com.zx.oa.controller;

import com.alibaba.excel.EasyExcel;
import com.zx.oa.dao.weekResult.WeekResultDao;
import com.zx.oa.dao.weekResult.WeekResultDaoImpl;
import com.zx.oa.entity.WeekResult;
import com.zx.oa.service.morningResult.MorningResultService;
import com.zx.oa.service.morningResult.MorningResultServiceImpl;
import com.zx.oa.service.weekResult.WeekResultService;
import com.zx.oa.service.weekResult.WeekResultServiceImpl;
import com.zx.oa.utils.PageSupport;
import com.zx.oa.utils.excel.ResultExcel;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebServlet("/weekresultservlet")
public class WeekResultServlet extends HttpServlet {


    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String date1 = request.getParameter("date");
        String result1 = request.getParameter("result");
        String stuId = request.getParameter("id");

        if ("modify".equals(request.getParameter("modify"))) {
            modifyWeekResult(date1,result1,stuId);
        }else if ("dele".equals(request.getParameter("dele"))){
            deleteWeekRestlt(stuId);
        }else if ("excel".equals(request.getParameter("excel"))){
            excelWeekResult(request,response);
        }
        
        //获取前段数据 用作查询条件
        String major = null;
        String classname = null;
        String namec = null;
        String date = null;
        String result = null;

        String m = request.getParameter("major");
        String c = request.getParameter("classname");
        String n = request.getParameter("namec");
        String d = request.getParameter("date");
        String r = request.getParameter("result");

        if (m != null && !"".equals(m)) {
            major = m;
        }
        if (c != null && !"".equals(c)) {
            classname = c;
        }
        if (n != null && !"".equals(n)) {
            namec = n;
        }
        if (d != null && !"".equals(d)) {
            date = d;
        }
        if (r != null && !"".equals(r)) {
            result = r;
        }


        //设置页面总数
        String pageIndex= request.getParameter("pageIndex");
        if(pageIndex==null){
            //第一次访问查询的servlet的时候没有pageIndex，赋默认值1
            pageIndex="1";
        }

        int currentPageNo=1;//当前页号
        int pageSize= PageSupport.pageSizeVal;//页面尺寸复制
        currentPageNo=Integer.parseInt(pageIndex);

        //存储页面信息
        PageSupport pages=new PageSupport();
        //设置页面尺寸
        pages.setPageSize(pageSize);

        //获取学生信息
        WeekResultService weekResultService = new WeekResultServiceImpl();

        //获去总数据条数
        int tCount = weekResultService.getTotalCount(major, classname, namec, date, result);

        //设置页面总记录数（注意此方法计算得到总页面数量）
        pages.setTotalCount(tCount);
        //获取到总的页数
        int totalPageCount=pages.getTotalPageCount();
        if(currentPageNo<1){
            currentPageNo=1;
        }else if(currentPageNo>totalPageCount){
            currentPageNo=totalPageCount;
        }

        List<WeekResult> weekResultList = weekResultService.checkWeekResult(major, classname, namec, date, result,currentPageNo,pageSize);

        //将当前页号存储到pages对象
        pages.setCurrentPageNo(currentPageNo);

        //放到请求域中，跳转到周考成绩查询页面
        HttpSession session = request.getSession();
        session.setAttribute("weekreportInfos",weekResultList);
        session.setAttribute("pageInfo",pages);
        session.setAttribute("major",major);
        session.setAttribute("classname",classname);
        session.setAttribute("name",namec);
        session.setAttribute("date",date);
        session.setAttribute("result",result);
        response.sendRedirect(request.getContextPath()+"/report/reportWeekResultInfo.jsp");

    }

    private void excelWeekResult(HttpServletRequest request, HttpServletResponse response) throws IOException {
        SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取到时间，以时间来定义下载Excel的名称
        String downLoadTime=sf.format(new Date());

        List<WeekResult> weekreportInfos=(List<WeekResult>)request.getSession().getAttribute("weekreportInfos");
        //设置Excel响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //下载的Excel文件名称
        String fileName="weekResult"+downLoadTime+".xlsx";
        response.setHeader("Content-disposition","allachment;filename="+fileName);
        EasyExcel.write(response.getOutputStream(),
                ResultExcel.class).sheet("周考信息").doWrite(weekreportInfos);
    }

    //删除成绩
    private void deleteWeekRestlt(String stuId) {
        MorningResultService morningResultService = new MorningResultServiceImpl();
        int id = Integer.parseInt(stuId);
        morningResultService.deleteResult(id);
    }

    //修改周考成绩
    private void modifyWeekResult(String date,String result,String stuId) {
        WeekResultService weekResultService = new WeekResultServiceImpl();
        weekResultService.modifyWeekResult(date, result, stuId);
    }




}
